package br.gendi.rcp;


import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import br.gendi.beans.BaseGenomica;
import br.gendi.core.Parser;
import br.gendi.dialog.ConfiguracoesBases;


/**
 * This sample class demonstrates how to plug-in a new
 * workbench view. The view shows data obtained from the
 * model. The sample creates a dummy model on the fly,
 * but a real implementation would connect to the model
 * available either in this or another plug-in (e.g. the workspace).
 * The view is connected to the model using a content provider.
 * <p>
 * The view uses a label provider to define how model
 * objects should be presented in the view. Each
 * view can present the same model objects using
 * different labels and icons, if needed. Alternatively,
 * a single label provider can be shared between views
 * in order to ensure that objects of the same type are
 * presented in the same way everywhere.
 * <p>
 */

public class SiteView extends ViewPart {
	
	public static final String ID = "br.gendi.rcp.siteView";


	private Label label = null;
	private Text text = null;
	private Button button = null;
	private Shell shell = null;


	public void createPartControl(final Composite parent) {
		shell = parent.getShell();
		GridData gridData6 = new GridData();
		gridData6.horizontalAlignment = GridData.BEGINNING;
		gridData6.verticalAlignment = GridData.CENTER;
		GridData gridData5 = new GridData();
		gridData5.horizontalAlignment = GridData.BEGINNING;
		gridData5.verticalAlignment = GridData.BEGINNING;
		GridData gridData4 = new GridData();
		gridData4.horizontalAlignment = GridData.FILL;
		gridData4.verticalAlignment = GridData.BEGINNING;
		GridData gridData3 = new GridData();
		gridData3.horizontalAlignment = GridData.FILL;
		gridData3.verticalAlignment = GridData.BEGINNING;
		GridData gridData2 = new GridData();
		gridData2.grabExcessHorizontalSpace = true;
		gridData2.verticalAlignment = GridData.CENTER;
		gridData2.horizontalAlignment = GridData.FILL;
		GridData gridData1 = new GridData();
		gridData1.grabExcessHorizontalSpace = true;
		gridData1.verticalAlignment = GridData.FILL;
		gridData1.grabExcessVerticalSpace = true;
		gridData1.horizontalAlignment = GridData.FILL;
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.CENTER;
		gridData.horizontalSpan = 2;
		gridData.horizontalAlignment = GridData.FILL;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 4;
		label = new Label(parent, SWT.NONE);
		label.setText("Endereço da base genômica:");
		label.setLayoutData(gridData6);
		text = new Text(parent, SWT.BORDER);
		text.setLayoutData(gridData2);
		//text.setText("http://www.cbs.dtu.dk/services/NetMHC");
		button = new Button(parent, SWT.NONE);
		button.setText("Adicionar Base");
		button.setImage(new Image(shell.getDisplay(), getClass().getResourceAsStream("/images/addsite.png")));
		button.setLayoutData(gridData);
		
		parent.setLayout(gridLayout);

		//buscar
		
		//adicionar base
		button.addSelectionListener(new SelectionListener() { 
			public void widgetDefaultSelected(SelectionEvent arg0) {

			}

			public void widgetSelected(SelectionEvent arg0) {
				final String texto = text.getText();

				if (texto.length() == 0) {
					MessageDialog.openError(shell, "EPIDI: Erro", "Sem site para adicionar.");
					return;
				}
				try {
					new URL(texto);
				} catch (MalformedURLException e) {
					MessageDialog.openError(shell, "EPIDI: Erro", "Endereço não é valido: " + texto + "\n\n"+e.toString());
					return;
				}
				Thread operationThread = new Thread() {
					public void run() {
						final BaseGenomica b = Parser.parseSite(texto);

						shell.getDisplay().asyncExec(new Runnable() {
							public void run() {
								ConfiguracoesBases c = new ConfiguracoesBases(b);
								writeToStatusLine("Nova base sendo configurada.");
							}
						});
					}
				};
				operationThread.start();
				writeToStatusLine("Pesquisa iniciada.");
			}
		});
	}

	@Override
	public void setFocus() {
	}

	private void writeToStatusLine(String aMessage) {
		final String msg = aMessage;

		getViewSite().getShell().getDisplay().asyncExec (new Runnable () {
			public void run () {
				getViewSite().getActionBars().getStatusLineManager().setMessage(msg);
			}
		});
	}
}